package com.xzbd.springbootmybatiesdm.controller;

import java.util.List;
import java.util.Map;

import com.xzbd.springbootmybatiesdm.common.response.R;
import com.xzbd.springbootmybatiesdm.domain.Event;
import com.xzbd.springbootmybatiesdm.service.EventService;
import com.xzbd.springbootmybatiesdm.utils.PageQueryUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("event")
public class EventController {

    @Autowired
    private EventService eventService;

    @GetMapping("list")
    public R list(@RequestParam Map<String, Object> params) {
        try {
            
            PageQueryUtils.parsePageParams(params);
            Integer total = eventService.count(params);
            List<Event> list = eventService.list(params);
            return R.pageDataOk(total, list);
        } catch (Exception e) {
            // e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @PutMapping("update")
    public R update(@RequestBody Event event){
        int record = eventService.update(event);
        if(record>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @DeleteMapping("delById")
    public R del(@RequestParam String id){
        int record = eventService.del(id);
        if(record>0){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @GetMapping("eventById")
    public R getEventById(@RequestParam("id") String id){
        Event record = eventService.getEventById(id);
        if(record != null){
            return R.dataOk(record);
        }else{
            return R.error();
        }
    }
    @PostMapping("/add/json")
    public R addJson(@RequestBody Event event){
        int add = eventService.add(event);
        if(add>0){
            return R.dataOk(event);
        }else{
            return R.error();
        }
        
    }

    @PostMapping("/add/form")
    public R addForm( Event event){
        int add = eventService.add(event);
        if(add>0){
            return R.dataOk(event);
        }else{
            return R.error();
        }
    }
}